#!/usr/bin/env python3
'''
###############################################################################
###############################################################################
##                                                                           ##
##     _    ___  ___   ___ ___ ___                                           ##
##    | |  | __ /   \ / __| _ | __|                                          ##
##    | |__| __  ( ) | (_ |  _|__ \                                          ##
##    |____|___ \___/ \___|_| \___/                                          ##
##                                    v 0.2 (Alpha)                          ##
##                                                                           ##
## FILE DESCRIPTION:                                                         ##
##                                                                           ##
## This routine is a RINEX path finder. If an observation file is found,     ##
## with the 4-letter spacecraft ID matching the user's config.txt inputs,    ##
## the filepath will be forwarded to 'rinxtr.py' and 'timing.py' for data    ##
## extraction. If observation files are found, but they are 'hatanaka'       ##
## compressed, then LEOGPS will run the hatanaka decompression executable    ##
## to decompress the file, from (.D) to (.O) extension.                      ##
##                                                                           ##
## INPUTS:                                                                   ##
##                                                                           ##
## The user-defined inputs dictionary object, generated by inpxtr.py         ##
##                                                                           ##
## OUTPUT:                                                                   ##
##                                                                           ##
## RINEX observation filepaths for both LEOs.                                ##
##                                                                           ##
## REMARKS:                                                                  ##
##                                                                           ##
## Yuki Hatanaka (hatagsi.go.jp) (GSI) wrote and maintains rnx2crx and       ##
## crx2rnx, which allows the user to compress/decompress, respectively, a    ##
## RINEX observation file into smaller ASCII format. The Hatanaka-compressed ##
## ASCII format version of a RINEX observation file is frequently used in    ##
## conjuction with the UNIX compress, zip, gzip or other general compression ##
## utilities to create a very small file for Internet transfer.              ##
##                                                                           ##
## AUTHOR MODIFIED: 06-12-2019, by Samuel Y.W. Low (Credits: Yuki Hatanaka)  ##
##                                                                           ##
###############################################################################
###############################################################################
'''

import os
import shutil
import subprocess

''' Get the names of required files, following AIUB CODE's nomenclature '''

def rnpath(inps):

    # Let us start by parsing out all relevant user configuration inputs.
    yy       = inps['dtstart_yy'] # The starting two-digit year number
    doy      = inps['dtstart_doy'] # The starting day of year in GPST
    cwd      = inps['cwd'] # Current working directory
    iwd      = cwd + '\\input\\'
    name1    = inps['name1'] # 4-letter ID of the first spacecraft
    name2    = inps['name2'] # 4-letter ID of the second spacecraft
    
    # Now, we get all necessary file paths, in the input folder.
    rnx1file = iwd + name1 + doy + '0.' + yy + 'O'
    rnx2file = iwd + name2 + doy + '0.' + yy + 'O'
    crx1file = iwd + name1 + doy + '0.' + yy + 'D'
    crx2file = iwd + name2 + doy + '0.' + yy + 'D'
    
    # Then, we get file paths for the hatanaka directory.
    rnx1hata = cwd + '\\utils\\hatanaka\\' + name1 + doy + '0.' + yy + 'O'
    rnx2hata = cwd + '\\utils\\hatanaka\\' + name2 + doy + '0.' + yy + 'O'
    crx1hata = cwd + '\\utils\\hatanaka\\' + name1 + doy + '0.' + yy + 'D'
    crx2hata = cwd + '\\utils\\hatanaka\\' + name2 + doy + '0.' + yy + 'D'
    
    # Check if there is a need for hatanaka decompression for LEO 1.
    if os.path.exists(rnx1file) == True:
        print('Decompressed RINEX obs file observed for ' + name1 + '\n')
    elif os.path.exists(crx1file) == True:
        print('Hatanaka compressed file observed for ' + name1)
        shutil.move( crx1file, crx1hata )
        os.chdir(cwd + '\\utils\\hatanaka\\')
        subprocess.call( 'crx2rnx.exe ' + name1 + doy + '0.' + yy + 'D' )
        shutil.move( crx1hata, crx1file )
        shutil.move( rnx1hata, rnx1file )
        if os.path.exists(rnx1file) == True:
            print('Hatanaka decompression successful for LEO1! \n')
        else:
            print('Decompression failed. Did you rename any folders? \n')
    else:
        print('Neither compressed nor decompressed RINEX files were found! \n')
        
    # Check if there is a need for hatanaka decompression for LEO 2.
    if os.path.exists(rnx2file) == True:
        print('Decompressed RINEX obs file observed for ' + name2 + '\n')
    elif os.path.exists(crx2file) == True:
        print('Hatanaka compressed file observed for ' + name2)
        shutil.move( crx2file, crx2hata )
        os.chdir(cwd + '\\utils\\hatanaka\\')
        subprocess.call( 'crx2rnx.exe ' + name2 + doy + '0.' + yy + 'D' )
        shutil.move( crx2hata, crx2file )
        shutil.move( rnx2hata, rnx2file )
        if os.path.exists(rnx2file) == True:
            print('Hatanaka decompression successful for LEO2! \n')
        else:
            print('Decompression failed. Did you rename any folders? \n')
    else:
        print('Neither compressed nor decompressed RINEX files were found! \n')    
    
    if os.path.exists(rnx1file) == True and os.path.exists(rnx2file) == True:
        return rnx1file, rnx2file
    else:
        print('Error, somehow RINEX observation files still not found!')
        print('Did you rename any folders accidentally? \n')
        return False